package com.genius.utils;

import com.genius.auth.qo.EmailQO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Arrays;

@Slf4j
@Service
public class EmailUtils {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送普通邮件
     * @param emailQO 邮件信息
     */
    public void commonEmail(EmailQO emailQO) {
        //创建普通邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送方
        message.setFrom(from);
        //邮件接收方
        message.setTo(emailQO.getUsers());
        //邮件标题
        message.setSubject(emailQO.getSubject());
        //邮件内容
        message.setText(emailQO.getContent());
        try {
            mailSender.send(message);
            log.info("邮件发送成功");
        } catch (MailException e) {
            log.info("邮件发送异常信息：{}", e.getMessage());
        }
    }

    /**
     * 发送HTML邮件
     * @param emailQO 邮件信息
     */
    public void htmlEmail(EmailQO emailQO) {
        //创建一个MINE消息
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
            //邮件发送方
            mimeMessageHelper.setFrom(from);
            //邮件接收方
            mimeMessageHelper.setTo(emailQO.getUsers());
            //邮件主题
            mimeMessageHelper.setSubject(emailQO.getSubject());
            //邮件内容 true 表示带有附件或HTML
            mimeMessageHelper.setText(emailQO.getContent(), true);
            mailSender.send(message);
            log.info("HTML邮件发送成功，接收方：{}", Arrays.toString(emailQO.getUsers()));
        } catch (MailException | MessagingException e) {
            log.info("HTML邮件发送失败，异常信息：{}", e.getMessage());
        }
    }

    /**
     * 发送附件邮件
     * @param emailQO 邮件信息
     * @param file 文件
     */
    public void enclosureEmail(EmailQO emailQO, File file) {
        //创建一个MINE消息
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //邮件发送方
            helper.setFrom(from);
            //邮件接收方
            helper.setTo(emailQO.getUsers());
            //邮件主题
            helper.setSubject(emailQO.getSubject());
            //邮件内容 true 表示带有附件或html
            helper.setText(emailQO.getContent(), true);
            FileSystemResource fileSystemResource = new FileSystemResource(file);
            String filename = fileSystemResource.getFilename();
            //添加附件
            assert filename != null;
            helper.addAttachment(filename, fileSystemResource);
            mailSender.send(message);
            log.info("附件邮件发送成功，接收方：{}", Arrays.toString(emailQO.getUsers()));
        } catch (MessagingException e) {
            log.info("附件邮件发送失败，异常信息：{}", e.getMessage());
        }
    }

}